<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="header-img">
          <img src="../assets/heima.png" alt>
          <span>电商后台管理系统</span>
        </div>
        <div>
          <el-button type="info" @click="loginOut">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside :width="isToggleFlag ? '64px' : '200px'">
          <div class="toggleMenu" @click="changeMenu">|||</div>
          <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :unique-opened="true" :collapse="isToggleFlag" :collapse-transition="false" :router="true" :default-active="activeName">
            <!-- 一级菜单部分 -->
            <el-submenu :index="index + ''" v-for="(item, index) in menusList" :key="index">
              <template slot="title">
                <i :class="iconList[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单部分 -->
              <el-menu-item :index="'/' + itemChil.path" v-for="(itemChil) in item.children" :key="itemChil.id" @click="saveActive('/' + itemChil.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{itemChil.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import {asideListApi} from './api/index.js'
export default {
  data: function() {
    return {
      menusList: [],
      iconList: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      isToggleFlag: false,
      activeName: ''
    }
  },
  async created() {
    const {data: res} = await asideListApi()
    if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
    this.menusList = res.data
    this.activeName = window.sessionStorage.getItem('active')
  },
  methods: {
    loginOut() {
      window.sessionStorage.removeItem("token");
      location.reload();
    },
    changeMenu() {
      this.isToggleFlag = !this.isToggleFlag
    },
    saveActive(activePath) {
      sessionStorage.setItem('active', activePath)
      this.activeName = activePath
    }
  }
};
</script>

<style lang="less" scoped>
.home-container,
.el-container {
  height: 100%;
}
.el-header {
  padding-left: 0;
  .header-img {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.el-header,
.el-footer {
  display: flex;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
  justify-content: space-between;
  align-items: center;
}

.el-aside {
  background-color: #333744;
  color: #333;
  text-align: left;
  user-select: none;
}
.el-menu {
  border: 0;
}
.el-main {
  background-color: #eaedfe;
  color: #333;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.iconfont {
  margin-right: 10px;
}
.toggleMenu {
  width: 100%;
  background-color: #4a5064;
  color: #fff;
  font-size: 10px;
  text-align: center;
  line-height: 24px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
